
import 'package:flutter_bilibili/db/fw_cache.dart';

enum HttpMethod { GET, POST, DELETE }

///基础请求
///配置数据：
///   1.域名
///   2.参数
///   3.地址
///   4.是否登录
///   5.鉴权
abstract class BaseRequest {

  /// 统一的域名
  String baseUrl() {
    return "api.bilibili.com";
  }

  String authorization() {
    return "authorization";
  }

  String getToken() {
    return FwCache.getInstance().get(authorization());
  }

  /// 路径参数，如：abc.com?a=1&b=2
  var pathParams;

  var useHttps = true;

  HttpMethod httpMethod();

  String path();

  String url() {
    Uri uri;
    var pathStr = path();

    /// 拼接path参数
    if (pathParams != null && httpMethod() == HttpMethod.GET) {
      if (pathStr.endsWith("/")) {
        pathStr = "$pathStr$pathParams";
      } else {
        pathStr = "$pathStr/$pathParams";
      }
    }

    /// http和https切换
    if (useHttps) {
      uri = Uri.https(baseUrl(), pathStr, params);
    } else {
      uri = Uri.http(baseUrl(), pathStr, params);
    }
    print('url:${uri.toString()}');
    return uri.toString();
  }

  bool needLogin();

  Map<String, String> params = Map();

  /// 添加参数
  BaseRequest add(String k, Object v) {
    params[k] = v.toString();
    return this;
  }

  Map<String, dynamic> header = {};

  /// 添加header
  BaseRequest addHeader(String k, Object v) {
    header[k] = v.toString();
    return this;
  }
}
